package com.qdxwx.data.service.card;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.mapper.card.CardCodeMapper;
import com.qdxwx.data.mapper.card.CardMapper;
import com.qdxwx.models.card.CardCode;
import com.qdxwx.models.card.CardCodeQuery;
import com.qdxwx.models.card.CardCodeVO;
import com.qdxwx.models.enums.CardStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 *CardCode接口
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class CardCodeServiceImpl implements CardCodeService {

	@Autowired
	private CardCodeMapper cardCodeMapper;

	@Autowired
	private CardMapper cardMapper;
	/**
	 * 添加CardCode
	 *
	 * @param cardCode 要添加的CardCode
	 * @return id
	 */
	@Override
	public int insert(CardCode cardCode) {
		return cardCodeMapper.insert(cardCode);
	}

	@Override
	public int insertBatch(List<CardCode> cardCodeList) {
		return cardCodeMapper.insertBatch(cardCodeList);
	}

	/**
	 * 根据id删除一个CardCode
	 *
	 * @param id 要删除的id
	 */
	@Override
	public int deleteById(long id) {
		return cardCodeMapper.deleteById(id);
	}

	/**
	 * 修改CardCode
	 *
	 * @param cardCode 要修改的CardCode
	 */
	@Override
	public int update(CardCode cardCode) {
		return cardCodeMapper.update(cardCode);
	}

	@Override
	public int updateUsed(CardCode cardCode) {
		cardCode.setStatus(CardStatus.USED.toString());
		cardCode.setUseTime(LocalDateTime.now());
		int count = cardCodeMapper.updateUsed(cardCode);
		if (count > 0) {
			cardMapper.updateUsedNum(cardCode.getCardId());
		}
		return count;
	}

	@Override
	public int overById(long id) {
		if (id < 1) return 0;
		return cardCodeMapper.overById(id);
	}

	@Override
	public int overByCardId(long cardId) {
		if (cardId < 1) return 0;
		return cardCodeMapper.overByCardId(cardId);
	}

	@Override
	public int closeByCardId(long cardId) {
		if (cardId < 1) return 0;
		return cardCodeMapper.closeByCardId(cardId);
	}

	/**
	 * 根据id获取单个CardCode对象
	 *
	 * @param id 要查询的id
	 * @return CardCode
	 */
	@Override
	public CardCode getById(long id) {
		if (id < 1) return null;
		return cardCodeMapper.selectById(id);
	}

	@Override
	public CardCode getByCode(String code, String pwd) {
		if (code == null || TKString.isEmpty(code)) {
			return null;
		}
		return cardCodeMapper.selectByCode(code, pwd);
	}

	@Override
	public List<CardCode> getList(CardCode cardCode, PageRequest page) {
		return cardCodeMapper.selectList(cardCode,page);
	}

	@Override
	public List<CardCodeVO> getVOList(CardCodeQuery cardCodeQuery, PageRequest page) {
		return cardCodeMapper.selectVOList(cardCodeQuery, page);
	}

	@Override
	public long count(CardCode cardCode) {
		return cardCodeMapper.count(cardCode);
	}

	@Override
	public int create(long cardId, long num) {
		int i;
		for (i = 1; i <= num; i++) {
			CardCode cardCode = new CardCode();
			cardCode.setCreateTime(LocalDateTime.now());
			cardCode.setCardId(cardId);
			cardCode.setStatus(CardStatus.INIT.toString());
			String ccode = getFixString((int) cardId, 4) + getFixString(i, 4) + TKString.getRanNumber(4);
			cardCode.setCardCode(ccode);
			cardCode.setCardCodePassword(TKString.getRanNumber(10));// 生成10位密码
			cardCodeMapper.insert(cardCode);
		}
		return i;
	}

	@Override
	public int updateCombine(long userId1, long userId2) {
		if(userId1 < 1 || userId2 < 1){
			throw new RuntimeException("更新卡卷：参数错误");
		}
		return cardCodeMapper.updateCombine(userId1,userId2);
	}

	private static String getFixString(int num, int len) {
		StringBuilder tp = new StringBuilder(num);
		if (len == 0) {
			return tp.toString();
		}
		if (tp.length() == len)
			return tp.toString();
		if (tp.length() > len)
			return tp.substring(0, len);
		for (int i = 0; i <= len / 4 + 1; i++) {
			tp.insert(0, "00000");
		}
		return tp.substring(tp.length() - len);
	}
}